<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-10-04
 * Time: 11:57
 */
namespace app\api\controller;

use app\common\controller\Apibase;
use app\common\model\Comment as cxModel;
use think\Loader;

class Comment extends Apibase{
    /**
     * 查询单条评论
     * 接收class和id   class为搜索字段，id为搜索值
     * @return \think\response\Json
     */
    public function getComment(){
        $res['code'] = '0';
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        $cxmodel = new cxModel();
        if(empty($getdata['class']) && $getdata['class'] != 'uid' && $getdata['class'] != 'aid' && $getdata['class'] != 'oid' && $getdata['class'] != 'pid'){
            $res['msg'] = '访问错误';
            return json($res);
        }
        if(empty($getdata['id'])){
            $res['msg'] = '访问错误';
            return json($res);
        }
        $cont = $cxmodel->where($getdata['class'],$getdata['id'])->find();
        $usermodel = new \app\common\model\UserData();
        $user = $usermodel->where('uid',$cont['uid'])->column('uid,username,uicon,uniname');
        $user = $user[$cont['uid']];
        $user['fusername'] = substr($user['username'],0,3)."****".substr($user['username'],7,4);
        if(!empty($user['uicon'])){
            $user['uicon'] = '/'.$this->webdb['updir'].'/'.$user['uicon'];
        }
        $cont['user'] = $user;
        if(empty($cont)){
            $res['msg'] = '暂无评论';
            return json($res);
        }
        $res['code'] = '1';
        $res['cont'] = $cont->toArray();
        $res['msg'] = '查询成功';
        return json($res);
    }

    /**
     * 本方法接收GET或POST值
     * 查询字段为AID或UID
     * 或查询字段不存在，则查询所有评论
     * 返回json数组 包括加密后用用户名及用户昵称及用户头像
     * @return \think\response\Json
     */
    public function getList(){
        $res['code'] = '0';
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        //  验证用户提交的数据
        $validate = Loader::validate('Comment');
        if (!$validate->scene('list')->check($getdata)) {
            $res['msg'] = $validate->getError();
            return json($res);
        }
        $cxmodel = new cxModel();
        $getlist = $cxmodel->getList($getdata);
        if($getlist == false){
            $res['msg'] = '暂无信息';
            return json($res);
        }
        $res['code'] = '1';
        $res['msg'] = '查询成功';
        $res['cont'] = $getlist;
        return json($res);
    }
    //  添加评论
    public function add(){
        $res['code'] = '0';
        if(!request()->isPost()){
            $res['msg'] = '访问错误！';
            return json($res);
        }
        $getdata = input();
        $getdata = datatrim($getdata);
        //  验证用户提交的数据
        $validate = Loader::validate('Comment');
        if (!$validate->scene('add')->check($getdata)) {
            $res['msg'] = $validate->getError();
            return json($res);
        }
        $cxmodel = new cxModel();
        if($cxmodel->allowField(true)->isUpdate(false)->save($getdata)){
            $res['code'] = "1";
            $res['msg'] = "评论成功！";
        }else{
            $res['msg'] = "评论失败！";
        }
        return json($res);
    }



}